package lt.ntec.danblog.server.blog;

import com.google.inject.Inject;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import lt.ntec.danblog.server.blog.model.BlogEntityFactory;
import lt.ntec.danblog.server.blog.model.Post;
import lt.ntec.danblog.server.blog.model.PostDAO;
import lt.ntec.danblog.server.blog.util.PostUtils;
import lt.ntec.danblog.server.core.model.*;
import lt.ntec.danblog.server.core.validator.ValidationException;
import lt.ntec.danblog.server.core.validator.Validator;

public class BlogService implements Service {

    @Inject
    private PostDAO postDao;
    
    @Inject
    @Nullable
    private User user;
    
    @Inject
    private TagFactory tagFactory;
    
    @Inject
    private TagDAO tagDAO;
    
    @Inject
    private BlogEntityFactory postFactory;

    @Override
    public Long createPost(String title, String content, String tag)
            throws ValidationException {

        Post post = postFactory.createPost(this.user, (PostUtils.
                generateUrlString(title)
                + "." + DOCUMENT_FIELD_ENDING));

        post.setTitle(title);
        postDao.save(post);
        
        Set<Tag> createTagsFromString =
                tagFactory.createTagsFromString(tag, post);
                
                
        post.setTags(createTagsFromString);

        post.setDate(new Date());
        post.setContent(content);

        Validator validator = new PostValidator();
        if (!validator.isValid(post)) {
            throw new PostValidator.PostValidationException(
                    validator.getMessages());
        }

        return postDao.save(post);
    }

    @Override
    public void deletePost(Long id) {
        Post byId = postDao.getById(id);
        tagDAO.deleteParentFromTags(byId);
        postDao.delete(byId);
    }

    @Override
    public void updatePost(Long id, String title, String content, String tag)
            throws BlogServiceException, ValidationException {

        Post post = postDao.getById(id);

        post.setTags(tagFactory.createTagsFromString(tag, post));
        post.setContent(content);
        post.setTitle(title);

        Validator validator = new PostValidator();
        if (!validator.isValid(post)) {
            throw new PostValidator.PostValidationException(
                    validator.getMessages());
        }

        postDao.save(post);

    }

    @Override
    public Long addComment(Long id, String content)
            throws BlogServiceException, ValidationException {

        Post post = postDao.getById(id);

        Comment comment = postFactory.createComment(this.user, post);
        comment.setContent(content);
        comment.setDate(new Date());

        post.addComment(comment);
        postDao.save(post);

        return comment.getId();
    }

    @Override
    public List<Post> getPosts() {
       return (List<Post>) postDao.getList();
    }
}
